package org.example.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.example.domain.dto.DTO;
import org.example.domain.po.*;
import org.example.service.ErpRoleService;
import org.example.service.ErpmaterialinventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/erpmaterialinventory")
public class ErpmaterialinventoryController {
    @Autowired
    private ErpmaterialinventoryService erpmaterialinventoryService;
    @Autowired

    private ErpRoleService roleService;

    @GetMapping("/findByPage")
    public R<Page<Erpmaterialinventory>>getMaterialInventory(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "2") Integer size,@RequestAttribute ErpUser user,
            Long depotId
    ){
        Page<Erpmaterialinventory> page = new Page<>(current, size);
            page = erpmaterialinventoryService.getMaterialInventory(page,user.getTenantId(), depotId);
            return R.success(page);

    }

    @GetMapping("/find")
    public R<List<Erpmaterialinventory>>findMaterialInventory(Long headerId){

        return R.success(erpmaterialinventoryService.getherderId(headerId));
    }
    // 控制器
    @GetMapping("/list")
    public R<List<Erpmaterialinventory>> getMaterialInventoryByBarCode(@RequestParam("headerId") Long headerId) {
        try {
            List<Erpmaterialinventory> list = erpmaterialinventoryService.getMaterialInventoryByBarCode(headerId);
            return R.success(list);
        } catch (Exception e) {
            return R.fail("查询失败：" + e.getMessage());
        }
    }


    @GetMapping("/listxq")
    public R<List<Erpmaterialinventory>>getMaterialInventoryByBarCode(Long headerId,@RequestAttribute ErpUser user){
        try {
            List<Erpmaterialinventory> list = erpmaterialinventoryService.getMaterialInventoryByBarCodexq(headerId);
            ErpRole roleByUserId = roleService.getRoleByUserId(user.getId());
            if(StrUtil.contains("6", roleByUserId.getPriceLimit())){
                list.forEach(item -> {
                    List<String>names= Arrays.asList("totalPrice","deposit","unitPrice","allPrice","taxLastMoney");
                    DTO.shieldPrice(item,names);
                });

            }
            return R.success(list);
        } catch (Exception e) {
            return R.fail("查询失败：" + e.getMessage());
        }

    }


}
